package com.handinfo.game;

import javax.microedition.lcdui.Graphics;


import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.uimenu.UIManager;
import com.handinfo.uimenu.UITools;
import com.handinfo.util.Tools;

/**
 * 装备类
 * 
 * @author venizeng
 * 
 */
public class ActorItemEquip extends ActorItem {
	// 宝石颜色
	public static final byte COLOR_JEWEL_NONE = 0;// 宝石颜色-红
	public static final byte COLOR_JEWEL_RED = 1;// 宝石颜色-红
	public static final byte COLOR_JEWEL_BLUE = 2;
	public static final byte COLOR_JEWEL_GREEN = 3;
	public static final byte COLOR_JEWEL_YELLOW = 4;
	// 宝石名称
	public static final String NAME_JEWEL_RED = "红宝石";
	public static final String NAME_JEWEL_BLUE = "蓝宝石";
	public static final String NAME_JEWEL_GREEN = "绿宝石";
	public static final String NAME_JEWEL_YELLOW = "黄宝石";
	public static final String NAME_JEWEL_FULING = "赋灵宝石";
	// 强化类型
	public static final byte TYPE_PHY = 1;// 物理强化
	public static final byte TYPE_MAGIC = 2;// 法术强化
	public static final byte TYPE_DEFENCE = 3;// 防御强化
	// Var
	public byte m_level_magic;// 物理强化等级
	public byte m_level_phy;// 法术强化等级
	public byte m_level_defence;// 防御强化等级
	// 宝石描述
	public byte[] m_color_jewels;// 宝石颜色
	public boolean[] m_isXiangqian_jewels;// 宝石镶嵌
	public short m_naijiu;// 耐久
	public short m_maxNaijiu;// 最大耐久

	/**
	 * 将武器（和策划确定了）分解 2012-4-16 下午03:06:23 venizeng
	 * 
	 * @return
	 */
	public boolean isCanResove() {
		return true;
	}

	public int drawName(Graphics g, int tempX, int tempY) {
		String temp = null;
		if (m_level_defence > 0) {
			temp = "+" + String.valueOf(m_level_defence);
			g.setColor(Tools.YELLOW);
			g.drawString(temp, tempX, tempY, 0);
			tempX += AbstractGameScreen.s_font.stringWidth(temp);
		} else {
			if (m_level_phy > 0) {
				temp = "+" + String.valueOf(m_level_phy);
				g.setColor(Tools.RED);
				g.drawString(temp, tempX, tempY, 0);
				tempX += AbstractGameScreen.s_font.stringWidth(temp);
			}
			if (m_level_magic > 0) {
				if (m_level_phy > 0) {
					temp = "/+" + String.valueOf(m_level_magic);
				} else {
					temp = "+" + String.valueOf(m_level_magic);
				}
				g.setColor(0x2d9cff);
				g.drawString(temp, tempX, tempY, 0);
				tempX += AbstractGameScreen.s_font.stringWidth(temp);
			}
		}
		super.drawName(g, tempX, tempY);
		tempX += AbstractGameScreen.s_font.stringWidth(m_name) +3;
		//
		if (m_maxNaijiu > 0) {
			g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
			temp = "耐";
			g.drawString(temp, tempX, tempY, 0);
			tempX += AbstractGameScreen.s_font.stringWidth(temp);
			//
			temp = String.valueOf(m_naijiu);
			g.drawString(temp, tempX, tempY, 0);
			tempX += AbstractGameScreen.s_font.stringWidth(temp);
			//
			temp = "/";
			g.drawString(temp, tempX, tempY, 0);
			tempX += AbstractGameScreen.s_font.stringWidth(temp);
			//
			temp = String.valueOf(m_maxNaijiu);
			g.drawString(temp, tempX, tempY, 0);
			tempX += AbstractGameScreen.s_font.stringWidth(temp) ;
		}
		return tempX ;
	}

	public static void drawJewel(Graphics g, int tempX, int tempY, ActorItemEquip equip) {
		if (equip.m_color_jewels == null) {
			return;
		}
		final int H_JEWEL = UIManager.IMAGE_JEWEL_RECT.getHeight();
		for (int i = 0, length = equip.m_color_jewels.length; i < length; i++) {
			ActorItemEquip.drawJewel(g, tempX, tempY, H_JEWEL, H_JEWEL, equip.m_color_jewels[i], equip.m_isXiangqian_jewels[i]);
			tempX += H_JEWEL + 2;
		}
	}

	/**
	 * 画宝石格子
	 * 
	 * @param color
	 * @param isXiangqian
	 * @param g
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 */
	private static void drawJewel(Graphics g, int x, int y, int w, int h, byte color, boolean isXiangqian) {
		int rectW = (UIManager.IMAGE_JEWEL_RECT.getWidth() >> 2);
		int rectH = UIManager.IMAGE_JEWEL_RECT.getHeight();
		int jW = (UIManager.IMAGE_JEWEL.getWidth() >> 2);
		int jH = UIManager.IMAGE_JEWEL.getHeight();
		int clipX = g.getClipX();
		int clipY = g.getClipY();
		int clipW = g.getClipWidth();
		int clipH = g.getClipHeight();
		g.setClip(x, y, rectW, rectH);
		switch (color) {
		case COLOR_JEWEL_RED:
			g.drawRegion(UIManager.IMAGE_JEWEL_RECT, 0 * rectW, 0, rectW, rectH, 0, x, y, 0);
			if (isXiangqian) {
				g.drawRegion(UIManager.IMAGE_JEWEL, 0 * jW, 0, jW, jH, 0, x + 2, y + 2, 0);
			}
			break;
		case COLOR_JEWEL_BLUE:
			g.drawRegion(UIManager.IMAGE_JEWEL_RECT, 3 * rectW, 0, rectW, rectH, 0, x, y, 0);
			if (isXiangqian) {
				g.drawRegion(UIManager.IMAGE_JEWEL, 3 * jW, 0, jW, jH, 0, x + 2, y + 2, 0);
			}
			break;
		case COLOR_JEWEL_GREEN:
			g.drawRegion(UIManager.IMAGE_JEWEL_RECT, 2 * rectW, 0, rectW, rectH, 0, x, y, 0);
			if (isXiangqian) {
				g.drawRegion(UIManager.IMAGE_JEWEL, 2 * jW, 0, jW, jH, 0, x + 2, y + 2, 0);
			}
			break;
		case COLOR_JEWEL_YELLOW:
			g.drawRegion(UIManager.IMAGE_JEWEL_RECT, 1 * rectW, 0, rectW, rectH, 0, x, y, 0);
			if (isXiangqian) {
				g.drawRegion(UIManager.IMAGE_JEWEL, 1 * jW, 0, jW, jH, 0, x + 2, y + 2, 0);
			}
			break;
		}
		g.setClip(clipX, clipY, clipW, clipH);
	}
}
